<?php
namespace Home\Controller;
use Think\Cache\Driver\Redis;
class MoneyController extends CommonController {

public function _initialize()
{
		$this->Is_login();        //检测用户登录信息,如果错误直接返回错误,终止程序
		$this->Is_power("money");        //检测用户权限,如果错误直接返回错误,终止程序
		$this->assign('heardadminData',session('heardadminData'));
}

//兑换操作
public function draw_time()
{
	$password = I('password');
	if (empty($password)||$password!="H123456") {
		$this->error('密码错误',"",1);
	}else{
		$db_member = M("member");
		$member = $db_member->where("monthtime>=3600")->field('uuid,monthtime,proportion')->select();
		foreach ($member as $key => $value) {
			M()->startTrans();
			$num = floor($value['monthtime']/3600);
			$money = $num*$value['proportion'];
			$db_member->lock(true)->where(array('uuid'=>$value['uuid']))->setInc('income',$money);// 加总收入
			$db_member->lock(true)->where(array('uuid'=>$value['uuid']))->setInc('balance',$money);// 加余额
			$db_member->lock(true)->where(array('uuid'=>$value['uuid']))->setDec('monthtime',$num*3600);// 减剩余时长
        M()->execute("UNLOCK TABLES");
			$data['time'] = time();
			$data['type'] = 3;
			$data['uuid'] = $value['uuid'];
			$data['amount'] = $money;
			$data['status'] = 1;
			$data['settlement'] = $num*3600;
			$res = M('income')->add($data);
			if($res){
				M()->commit();
			}else{
				M()->rollback();
			}
		}
		$this->adminlog("兑换时长");
		$this->success('兑换成功',"/Home/Money/draw_view",1);
	}
}
//兑换页面
public function draw_view()
{
		$this->display();
}
//一键结算列表
public function draw_list()
{
		$db_income = M("income i");
		$where = "i.type = 3";
		$count = $db_income->where($where)->order('id desc')->count();
		$p = getpage($count, 20);
		$incomelist = $db_income->where($where)->join('__MEMBER__ m ON i.uuid = m.uuid')->join('__AUTHENTICATION__ a ON i.uuid = a.uuid')->field('i.id,i.uuid,i.amount,i.time,i.settlement/3600 settlement,m.nickname,a.name')->order('id desc')->limit($p->firstRow, $p->listRows)->select();
		// p($incomelist);exit();
		$this->assign('incomelist',$incomelist);
		$this->assign('page', $p->show());
		$this->display();
}
	//签约主播列表
	public function draw_user()
	{
		// p($_POST);exit();
		$db_member = M("member");
		$labelkey = I('get.labelkey');
		$user_name = I('get.user_name');
		$this->user_name = $user_name;
		$where = " 1=1 ";
		if (!empty($user_name)) {$where .= " and (nickname like '%$user_name%' or uuid like '%$user_name%' or username like '%$user_name%')";}
		// echo $where;exit();
		$count = $db_member->where(array('status'=>9,'authentication'=>1))->where($where)->count();
		$p = getpage($count, 20);
		$userinfo = $db_member->where(array('status'=>9,'authentication'=>1))->where($where)->limit($p->firstRow, $p->listRows)->select();
		foreach ($userinfo as $key => &$value) {
			$value['monthtime'] = sprintf('%.2f', $value['monthtime']/3600);
		}
		$page = $p->show();
		// 获取查询参数
		$map['user_name'] = empty($user_name)?"":$user_name;
		$p->parameter = "";
		foreach($map as $key=>$val) {
				$p->parameter .= "$key=".urlencode($val)."&";
		}
		$this->assign('userinfo',$userinfo);
		$this->assign('page', $page);
		$this->display();
	}
//提现列表
public function balance_list()
{
	$this->strat = !empty(I('strat')) ? strtotime(urldecode(I('strat'))) : 0;
	$this->end = !empty(I('end')) ? strtotime(urldecode(I('end'))) : time();
	$keyword = I('param.keyword');
	$where['b.time'] = array("BETWEEN",array($this->strat,$this->end));
	$where_k = '';
	$this->keyword = $keyword;
	if(!empty($keyword)){
		$where_k = "(nickname like '%$keyword%' or m.uuid like '%$keyword%')";
	}
	$db_balancemoney = M("balance b");
	$db_member = M("member");
	$count = $db_balancemoney->join('__MEMBER__ m ON b.uuid = m.uuid')->where($where)->where($where_k)->order('b.id desc')->count();
	$p = getpage($count, 20);
	$balancemoneyinfo = $db_balancemoney->join('__MEMBER__ m ON b.uuid = m.uuid')->where($where)->where($where_k)->field('b.*,m.username,m.nickname')->order('id desc')->limit($p->firstRow, $p->listRows)->select();
	// p($balancemoneyinfo);exit();
	// 获取查询参数
	$page = $p->show();
	$map['keyword'] = empty($keyword)?"":$keyword;
	$map['strat'] = $this->strat;
	$map['end'] = $this->end;
	$p->parameter = "";
	foreach($map as $key=>$val) {
		$p->parameter .= "$key=".urlencode($val)."&";
	}
	$this->assign('balancemoneyinfo',$balancemoneyinfo);
	// $this->assign('member',$member);
	$this->assign('page',$page);
	$this->display();
}
public function balance_daochu()
{
	$strat = !empty(I('strat')) ? strtotime(I('strat')) : 1;
	$end = !empty(I('end')) ? strtotime(I('end')) : time();
	$where['b.time'] = array("BETWEEN",array($strat,$end));
	$filename = "主播提现数据";
	$excelarr = array("提现编号","主播昵称","用户编号","手机号","提现金额","提现时间");
	$db_balancemoney = M("balance b");
	$db_member = M("member");
	$balancelist = $db_balancemoney->join('__MEMBER__ m ON b.uuid = m.uuid')->field('b.*,m.username,m.nickname')->where($where)->order('id desc')->select();
	foreach ($balancelist as $key => $value) {
		$excellist[$key]['trade_no'] = $value['trade_no'];
		$excellist[$key]['nickname'] = $value['nickname'];
		$excellist[$key]['uuid'] = $value['uuid'];
		$excellist[$key]['username'] = $value['username'];
		$excellist[$key]['amount'] = $value['amount'];
		$excellist[$key]['time'] = date("Y-m-d H:i:s",$value['time']);
	}
	// p($excellist);exit();
	output_excel($filename.time(),$excelarr,$excellist);
	$this->adminlog("导出主播提现数据");
}
//开票列表
public function invoice_list()
{
		$db_wemediainvoice = M('wemediainvoice i');
		$count = $db_wemediainvoice->order('id desc')->count();
		$p = getpage($count, 20);
		$invoicelist = $db_wemediainvoice->join('__WEMEDIA__ w ON i.wid = w.wid')->field('w.username,i.*')->order('id desc')->limit($p->firstRow, $p->listRows)->select();
		$this->assign('invoicelist',$invoicelist);
		$this->assign('page', $p->show());
		$this->display();
}
//开票详情
	public function invoice_message()
	{
		$id = I('get.id');
		$db_wemediainvoice = M('wemediainvoice');
		$invoicedata = $db_wemediainvoice->where(array('id'=>$id))->find();
		$this->assign('invoicedata',$invoicedata);
		$this->display();
	}
	//确认开票
	public function invoice_edit()
	{
		$id = I('post.id');
		$data['status'] = 1;
		$res = M("wemediainvoice")->where(array('id'=>$id))->save($data);//更新数据
		if ($res) {
		$this->adminlog("开票操作".$id);
			$this->success('开票成功',"/Home/Money/invoice_list",1);
		}else{
		  $this->error('开票失败',"",1);
		}
	}


	//财务列表
	public function expend_list()
	{
		$wemediaincome = M("wemediaincome i");
		$db_wemedia = M("wemedia m");
		$where = " 1=1 ";
		$where .= " and (i.status = 3 or i.status = 4 or i.status = 5)";
		$username = I('get.username');
		if (!empty($username)) {$where .= " and w.username = $username";}
		$count = $wemediaincome
		->join('__WEMEDIA__ w ON i.wid = w.wid')
		->field("i.id")
		->where($where)->count();
		$p = getpage($count, 20);
		$expend_list = $wemediaincome
		->join('__WEMEDIA__ w ON i.wid = w.wid')
		->order("i.id desc")
		->field("w.username,w.wid,i.id,i.creationtime,i.status,i.money,i.number")
		->where($where)->limit($p->firstRow, $p->listRows)->select();
		$page = $p->show();
		$map['username'] = I('get.username');
		$p->parameter = "";
		foreach($map as $key=>$val) {
				$p->parameter .= "$key=".urlencode($val)."&";
		}
		$userlist = M('wemedia')->where("status=2")->field("username")->select();
		foreach ($expend_list as $key => &$value) {
			if ($value['status'] == 7) {
				$value['username'] = M('media')->field("media_tel")->where("mmid = $value[wid]")->find()['media_tel'];
			}
		}
		$this->assign('userlist',$userlist);
		$this->assign('expend_list',$expend_list);
		$this->assign('page', $page);
		$this->display();
	}
	//财务列表
	public function income_list()
	{
		$wemediaincome = M("wemediaincome i");
		$db_wemedia = M("wemedia m");
		$where = " 1=1 ";
		$where .= " and (i.status = 1 or i.status = 2)";
		$username = I('get.username');
		if (!empty($username)) {$where .= " and w.username = $username";}
		$count = $wemediaincome
		->join('__WEMEDIA__ w ON i.wid = w.wid')
		->field("i.id")
		->where($where)->count();
		$p = getpage($count, 20);
		$income_list = $wemediaincome
		->join('__WEMEDIA__ w ON i.wid = w.wid')
		->order("i.id desc")
		->field("w.username,w.wid,i.id,i.creationtime,i.status,i.money,i.number")
		->where($where)->limit($p->firstRow, $p->listRows)->select();
		$page = $p->show();
		$map['username'] = I('get.username');
		$p->parameter = "";
		foreach($map as $key=>$val) {
				$p->parameter .= "$key=".urlencode($val)."&";
		}
		$userlist = M('wemedia')->where("status=2")->field("username")->select();
		foreach ($income_list as $key => &$value) {
			if ($value['status'] == 6) {
				$value['username'] = M('media')->field("media_tel")->where("mmid = $value[wid]")->find()['media_tel'];
			}
			if($value['status'] == 1){
				$messageobj = json_decode(M('balanceqrcode')->where("number = $value[number]")->getField('message'));
				$value['number'] = $messageobj->trade_no;
			}
		}
		$this->assign('userlist',$userlist);
		$this->assign('income_list',$income_list);
		$this->assign('page', $page);
		$this->display();
	}

	//字数财务列表
	public function wordexpend_list()
	{
		$wemediaincome = M("wemediaincome i");
		$db_wemedia = M("wemedia m");
		$where = " 1=1 ";
		$where .= " and i.status = 7";
		$username = I('get.username');
		if (!empty($username)) {$where .= " and w.username = $username";}
		$count = $wemediaincome
		->join('__WEMEDIA__ w ON i.wid = w.wid')
		->field("i.id")
		->where($where)->count();
		$p = getpage($count, 20);
		$expend_list = $wemediaincome
		->join('__WEMEDIA__ w ON i.wid = w.wid')
		->order("i.id desc")
		->field("w.username,w.wid,i.id,i.creationtime,i.status,i.money,i.number")
		->where($where)->limit($p->firstRow, $p->listRows)->select();
		$page = $p->show();
		$map['username'] = I('get.username');
		$p->parameter = "";
		foreach($map as $key=>$val) {
				$p->parameter .= "$key=".urlencode($val)."&";
		}
		$userlist = M('wemedia')->where("status=2")->field("username")->select();
		foreach ($expend_list as $key => &$value) {
			if ($value['status'] == 7) {
				$value['username'] = M('media')->field("media_tel")->where("mmid = $value[wid]")->find()['media_tel'];
			}
		}
		$this->assign('userlist',$userlist);
		$this->assign('expend_list',$expend_list);
		$this->assign('page', $page);
		$this->display();
	}
	//字数财务列表
	public function wordincome_list()
	{
		$wemediaincome = M("wemediaincome i");
		$where = " 1=1 ";
		$where .= " and i.status in (6,13)";
		$username = I('get.username');
		$this->username = $username;
		if (!empty($username)) {$where .= " and (m.mmid like '%$username%' or m.media_name like '%$username%' or m.media_tel like '%$username%')";}
		$count = $wemediaincome
		->join('__MEDIA__ m ON i.wid = m.mmid')
		->where($where)->count();
		$p = getpage($count, 20);
		$income_list = $wemediaincome
			->join('__MEDIA__ m ON i.mid = m.mmid')
			->order("i.id desc")
			->field("m.media_tel,m.media_name,m.mmid,m.wid,i.id,i.creationtime,i.status,i.money,i.grantmoney,i.signmoney,i.serial_number,i.number")
			->where($where)->limit($p->firstRow, $p->listRows)->select();
		//取出status为12的（老的后台赠送的分两条存的）
		$old_grant = $wemediaincome->where(['status'=>12])->getField('number,money',true);
		foreach ($income_list as &$v){
			if($old_grant[$v['number']]){
				$v['grantmoney'] = $old_grant[$v['number']];
			}
		}
		$page = $p->show();
		$map['username'] = I('get.username');
		$p->parameter = "";
		foreach($map as $key=>$val) {
				$p->parameter .= "$key=".urlencode($val)."&";
		}
		$this->assign('income_list',$income_list);
		$this->assign('page', $page);
		$this->display();
	}
}